﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FrameworkIvy.App_Code
{
    public enum InputDateTimeFormat
    {
        DD_MM_YYYY = 1,
        MM_DD_YYYY = 2,
        DD_MM_YYYY_HH_mm_ss = 3,
        DD_MM_YYYY_hh_mm_ss_tt = 4,
        MM_DD_YYYY_HH_mm_ss = 5,
        MM_DD_YYYY_HH_mm_ss_tt = 6
    }
    public class DateTimeUtility
    {
        #region  Variable & constant
        // All constants and local variable should be declared here
        // TRY NOT TO CREATE PUBLIC VARIABLES INSTEAD USE PROPERTIES FOR THAT PURPOSE.
        // Variable naming convention :- 
        // The naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Naming convention for all local variable should start with underscore "_"
        // Naming convention for all local constant should start with "_The"
        // Example :    string _CustomerCode = string.Empty;
        // Example :    const string _TheDefaultSmtpServer = "DefaultSmtpServer";
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion  Variable & constant

        #region Constructor & Destructor
        // Cut and Copy the default constructor for the class inside this region.
        // Generally not required in UI layer.
        // Example:
        // public SampleClassTemplate()
        // {

        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Constructor & Destructor

        #region Properties
        // Any value/variable that needs to be exposed outside the class we need to create a property.
        // Avoid creating public variables rather create public properties.
        // UI layer may have public custom properties. these are very handy for User Controls.
        // Property naming convention :- Properties will be exposed outside the class, 
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // All properties should have a corresponding local variable 
        // and the property name and local variable name should match.
        // Example :
        // public string CustomerCode
        // {
        //     get
        //     {
        //         return _CustomerCode;
        //     }
        //     set
        //     {
        //         _CustomerCode = value;
        //     }
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Properties

        #region Events
        // Generally UI layer will have public events.
        // Cut and Copy the default generated events for the class inside this region.
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Events

        #region Public Methods
        // Any method that needs to be exposed outside the class will be created inside this region.
        // Public Method naming convention :- Public Method will be exposed outside the class, 
        // The input and output parameters are also going to be exposed outside the class
        // the naming should be self explanatory, make it long to explain the purpose 
        // and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Example :
        // public bool RegisterNewCustomer(Customer Customer)
        // {
        //     bool IsCustomerDataSaved = false;
        //     //TO DO logic to save data
        //     IsCustomerDataSaved = InsertCustomerDataUsingDao(Customer);
        //     return IsCustomerDataSaved;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================

        public static Nullable<DateTime> ConvertStringToDateTime(string InputDateTimeString, InputDateTimeFormat InputFormat)
        {
            Nullable<DateTime> _OutputDateTime = null;
            //DateTime _OutputDateTime = new DateTime(0, 0, 0, 0, 0, 0);

            if (!string.IsNullOrEmpty(InputDateTimeString))
            {
                string[] _DateTimeSplitter;


                int _Day = 0;
                int _Month = 0;
                int _Year = 0;

                int _Hour = 0;
                int _Minutes = 0;
                int _Seconds = 0;

                //_OutputDateTime = new DateTime(0, 0, 0, 0, 0, 0);

                string _AmPm = string.Empty;

                if (InputDateTimeString.Contains("/"))
                {
                    _DateTimeSplitter = InputDateTimeString.Split('/');
                }
                else
                {
                    _DateTimeSplitter = InputDateTimeString.Split('-');
                }
                if (_DateTimeSplitter.Length >= 3)
                {
                    if (InputFormat == InputDateTimeFormat.DD_MM_YYYY)
                    {
                        _Day = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Month = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);
                    }
                    if (InputFormat == InputDateTimeFormat.MM_DD_YYYY)
                    {
                        _Month = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Day = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);
                    }
                    if (InputFormat == InputDateTimeFormat.DD_MM_YYYY_HH_mm_ss)
                    {
                        _Day = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Month = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);

                    }
                    if (InputFormat == InputDateTimeFormat.MM_DD_YYYY_HH_mm_ss)
                    {
                        _Month = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Day = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);
                    }
                    if (InputFormat == InputDateTimeFormat.DD_MM_YYYY_hh_mm_ss_tt)
                    {
                        _Day = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Month = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);
                    }
                    if (InputFormat == InputDateTimeFormat.MM_DD_YYYY_HH_mm_ss_tt)
                    {
                        _Month = Convert.ToInt32(_DateTimeSplitter[0]);
                        _Day = Convert.ToInt32(_DateTimeSplitter[1]);
                        _Year = Convert.ToInt32(_DateTimeSplitter[2]);
                    }
                }
                _OutputDateTime = new DateTime(_Year, _Month, _Day, _Hour, _Minutes, _Seconds);
            }

            return _OutputDateTime;
        }

        #endregion Public Methods

        #region Private Methods
        // All local class level methods will be inside this region. Mostly these are helper methods.
        // Private Method naming convention :- The naming should be self explanatory, 
        // make it long to explain the purpose and use PascalCase to seperate words as shown in the example below.
        // DO NOT USE ANY ABBRIVIATION use full word (like "Customer" instead of "Cust"
        // Input parameter will have a "p" as prefix. 
        // This will help the parameter variable that the value is originated outside this private method.
        // Example :
        // public bool InsertCustomerDataUsingDao(Customer pCustomer)
        // {
        //     bool IsCustomerDataInsertedSuccessfully = false;
        //     //TO DO logic to save data
        //     //....
        //     //....
        //     return IsCustomerDataInsertedSuccessfully;
        // }
        // ==================== START YOUR CODE FROM NEXT LINE ========================


        #endregion Private Methods
    }
}